import java.util.concurrent.CountDownLatch

import akka.actor.{ActorRef, ActorSystem, Props}
import message.{CloseClient, NodeList, ShowNode}

object Server {
    val actorSystem = ActorSystem.create("server")

    var zkClientList = List[ActorRef]()

    var countDownLatch: CountDownLatch = null

    def start(clientNum: Int): Unit ={
        countDownLatch = new CountDownLatch(clientNum)
        for(_ <- 1 to clientNum) {
            createZkClient
        }
        countDownLatch.await()
        nodeList
        showNode
        closeClient
    }

    def createZkClient: Unit = {
        zkClientList ::= actorSystem.actorOf(Props.create(classOf[ZkClient]))
    }

    def zkClientRef = zkClientList.last

    def showNode: Unit ={
        zkClientRef.tell(ShowNode, null)
    }

    def closeClient: Unit = {
        zkClientList.foreach(_.tell(CloseClient, null))
    }

    def nodeList: Unit = {
        zkClientRef.tell(NodeList, null)
    }

}
